//
//  UIViewController+camera.m
//  XPhoto
//
//  Created by sentinel on 2020/4/3.
//  Copyright © 2020 baboy. All rights reserved.
//

#import "UIViewController+camera.h"
#import "CameraViewController.h"
#import "BKSoloNavigationController.h"

@interface UIViewController()

@end

@implementation UIViewController(camera)


- (NSInteger)maxSelectedPhotoCount{
    return MAX_PHOTO_COUNT_PER_POST;
}
- (void) takePhoto{
    CameraViewController *vc = [[CameraViewController alloc] init];
    vc.delegate = self;
    vc.maxVideoCount = 1;
    vc.maxSelectedCount = 12;
    
    BKSoloNavigationController *nav = [[BKSoloNavigationController alloc] initWithRootViewController:vc];
    nav.modalPresentationStyle = UIModalPresentationFullScreen;
    [self presentViewController:nav animated:YES completion:^{
        
    }];
    return;
    
//    MediaEditViewController *vc = [[MediaEditViewController alloc] initWithPhotos:nil];
//    BKNavigationController *nav = [[BKNavigationController alloc] initWithRootViewController:vc];
//    nav.modalPresentationStyle = UIModalPresentationFullScreen;
//    [self presentViewController:nav animated:YES completion:^{
//        
//    }];
//    return;
    BKActionSheet *actionSheet = [[BKActionSheet  alloc] initWithItems:@[
                                                                          @{
                                                                              @"title":NSLocalizedString(@"photo", @"按钮"),
                                                                              @"subtitle":NSLocalizedString(@"select_from_photo_lib", @"按钮"),
                                                                              @"tag":@ACTION_SHEET_INDEX_PHOTO_LIB
                                                                              },
                                                                          @{
                                                                              @"title":NSLocalizedString(@"take_photo", @"按钮"),
                                                                              @"subtitle":NSLocalizedString(@"take_photo_batched", @"按钮"),
                                                                              @"tag":@ACTION_SHEET_INDEX_TAKE_PHOTO
                                                                              },
                                                                          @{
                                                                              @"title":NSLocalizedString(@"shoot_video", @"按钮"),
                                                                              @"tag":@ACTION_SHEET_INDEX_TAKE_VIDEO
                                                                              }
                                                                          ]
                                                               delegate:self
                                                      cancelButtonTitle:NSLocalizedString(@"cancel", "按钮")];
    [actionSheet show];
}
- (void)actionSheet:(BKActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex data:(id)data{
    DLOG(@"actionSheet:%ld,%@", buttonIndex, data);
    NSInteger tag = [[data valueForKey:@"tag"] intValue];
    switch (tag) {
        case ACTION_SHEET_INDEX_PHOTO_LIB:{
            PhotoLibViewController *vc = [[PhotoLibViewController alloc] init];
            vc.delegate = self;
//            vc.maxSelectedCount = [self maxSelectedPhotoCount];
            BKSoloNavigationController *nav = [[BKSoloNavigationController alloc] initWithRootViewController:vc];
//            BKNavigationController *nav = [[BKNavigationController alloc] initWithRootViewController:vc];
            nav.modalPresentationStyle = UIModalPresentationFullScreen;
            [self presentViewController:nav animated:YES completion:^{
                
            }];
            break;
        }
        case ACTION_SHEET_INDEX_TAKE_PHOTO:{
//            PhotoCameraViewController *imagePicker = [PhotoCameraViewController imagePickerControllerForTakePicture];
//            imagePicker.modalPresentationStyle = UIModalPresentationFullScreen;
//            imagePicker.photoEditDelegate = self;
//            imagePicker.edit = YES;
//            [self presentViewController:imagePicker animated:YES completion:^{
//
//            }];
            CameraViewController *vc = [[CameraViewController alloc] init];
            vc.delegate = self;
            vc.maxVideoCount = 1;
            vc.maxSelectedCount = 12;
            
            BKSoloNavigationController *nav = [[BKSoloNavigationController alloc] initWithRootViewController:vc];
            nav.modalPresentationStyle = UIModalPresentationFullScreen;
            [self presentViewController:nav animated:YES completion:^{
                
            }];
            break;
        }
        case ACTION_SHEET_INDEX_TAKE_VIDEO:{
            PhotoCameraViewController *imagePicker = [PhotoCameraViewController imagePickerControllerForShootVideo];
            imagePicker.modalPresentationStyle = UIModalPresentationFullScreen;
            imagePicker.photoEditDelegate = self;
            imagePicker.edit = YES;
            [self presentViewController:imagePicker animated:YES completion:^{
                
            }];
            break;
        }
            
        default:
            break;
    }
}
-(void)actionSheetCanceled:(BKActionSheet *)actionSheet{
    DLOG(@"actionSheetCanceled");
    
}
- (void)photoEditViewController:(id)controller didEditPhotos:(NSArray*)photos{
    
}

- (void)photoLibViewController:(id)photoLibViewController didSelectPhotos:(NSArray<PhotoAsset *> *)photoAssets{
    DLOG(@"%@",photoAssets);
    MediaEditViewController *vc = [[MediaEditViewController alloc] initWithPhotos:photoAssets];
    BKNavigationController *nav = [[BKNavigationController alloc] initWithRootViewController:vc];
    nav.modalPresentationStyle = UIModalPresentationFullScreen;
    [self presentViewController:nav animated:YES completion:^{
        
    }];
}
@end
